package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.AreaMapper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.UserMapper;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.dto.VendingMachineDto;
import com.heima.pojo.dto.VendingMachineSearchDto;
import com.heima.pojo.entity.Area;
import com.heima.pojo.entity.Node;
import com.heima.pojo.entity.User;
import com.heima.pojo.entity.VendingMachine;
import com.heima.pojo.vo.VendingMachineSearchVo;
import com.heima.pojo.vo.VendingMachineVo;
import com.heima.service.VendingMachineService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ljk
 * @since 2024-10-23
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements VendingMachineService {
    private final VendingMachineMapper vmMapper;
    private final UserMapper userMapper;
    private final NodeMapper nodeMapper;
    private final AreaMapper areaMapper;

    @Override
    public VendingMachineSearchVo pageQuery(VendingMachineSearchDto dto) {
        IPage<VendingMachineVo> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        IPage<VendingMachineVo> list = vmMapper.selectPageVo(page, dto);
        VendingMachineSearchVo vo = VendingMachineSearchVo.builder()
                .pageIndex(String.valueOf(dto.getPageIndex()))
                .pageSize(String.valueOf(dto.getPageSize()))
                .totalCount(String.valueOf(list.getTotal()))
                .totalPage(String.valueOf(list.getPages()))
                .currentPageRecords(list.getRecords())
                .build();
        return vo;
    }

    @Override
    public void addVM(VendingMachineDto dto) {
        //准备数据
        Node node = nodeMapper.selectById(dto.getNodeId());
        LambdaQueryWrapper<Area> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Area::getAdCode, node.getAreaCode());
        Area area = areaMapper.selectOne(queryWrapper);
        String innerCode = vmMapper.findNewVMInnerCode();
        User user = userMapper.selectById(dto.getCreateUserId());
        //封装数据
        VendingMachine vendingMachine = VendingMachine.builder()
                .nodeId(Long.valueOf(dto.getNodeId()))
                .createUserId(Long.valueOf(dto.getCreateUserId()))
                .vmType(dto.getVmType())
                .businessId(node.getBusinessId())
                .ownerId(node.getOwnerId())
                .ownerName(node.getOwnerName())
                .regionId(node.getRegionId())
                .cityCode(area.getCityCode())
                .areaId(area.getId())
                .innerCode(String.format("%08d",Integer.parseInt(innerCode)+1))
                .vmStatus(0)
                .createUserName(user.getUserName())
                .build();
        vmMapper.insert(vendingMachine);
//        好丑的代码
//        VendingMachine vendingMachine = new VendingMachine();
//        vendingMachine.setNodeId(Long.valueOf(dto.getNodeId()));
//        vendingMachine.setCreateUserId(Long.valueOf(dto.getCreateUserId()));
//        vendingMachine.setVmType(dto.getVmType());
//        vendingMachine.setBusinessId(node.getBusinessId());
//        vendingMachine.setOwnerId(node.getOwnerId());
//        vendingMachine.setOwnerName(node.getOwnerName());
//        vendingMachine.setRegionId(node.getRegionId());
//        vendingMachine.setCityCode(area.getCityCode());
//        vendingMachine.setAreaId(area.getId());
//        String formattedNum = String.format("%08d",Integer.parseInt(innerCode)+1);
//        vendingMachine.setInnerCode(formattedNum);
//        vendingMachine.setVmStatus(0);
//        vendingMachine.setCreateUserName(user.getUserName());
//        vmMapper.insert(vendingMachine);
    }
}
